package example;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;

import model.Player;

/**
 * PlayerViewController
 * 
 * @author Patrick
 * @sinde 05.12.2011
 * 
 */
public class PlayerViewController implements ActionListener {

    private static PlayerViewController instance;
    private static AbstractButton play;
    private static JButton stop;
    private static JButton back;
    private static AbstractButton next;
    private static AbstractButton pause;
    private static Player player;

    private PlayerViewController() throws IOException,
            LineUnavailableException, UnsupportedAudioFileException {
        PlayerViewController.player = new Player(new File("src\\example\\Sleep Away.mp3"));
    }

    /**
     * Prototyp for Playing with the Player-Model mp3-files
     * 
     * @author Patrick
     * @since 05.12.2011
     * 
     * @param args
     */
    public static void main(String[] args) {
        PlayerViewController tester;
        try {
            tester = getInstance();

            JFrame frame = new JFrame("Player");

            play = new JButton("Play");
            play.addActionListener(tester);
            stop = new JButton("Stop");
            stop.addActionListener(tester);
            pause = new JButton("Pause");
            pause.addActionListener(tester);
            next = new JButton("Next");
            next.addActionListener(tester);
            back = new JButton("Back");
            back.addActionListener(tester);

            frame.getContentPane().setLayout(new GridLayout());
            frame.getContentPane().add(play);
            frame.getContentPane().add(stop);
            frame.getContentPane().add(pause);
            frame.getContentPane().add(next);
            frame.getContentPane().add(back);

            frame.setVisible(true);
            frame.setSize(400, 100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @author Patrick
     * @since 05.12.2011
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == play) {
            try {
                player.play();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (LineUnavailableException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedAudioFileException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (e.getSource() == stop) {
            try {
                player.stop();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (e.getSource() == pause) {
            try {
                player.pause();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (e.getSource() == next) {
            try {
                player.next();
            } catch (LineUnavailableException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedAudioFileException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else if (e.getSource() == back) {
            try {
                player.back();
            } catch (LineUnavailableException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedAudioFileException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    private static PlayerViewController getInstance() throws IOException,
            LineUnavailableException, UnsupportedAudioFileException {
        if (instance == null)
            return new PlayerViewController();
        else
            return instance;
    }
}
